//////////////////////////////////////////////////////////////////////////
// FileName:ucPhraseDropDownList
// CreateBy:DragonXiong
// CreateDate:2010-05-20
// Description:
//		ucPhraseDropDownList usercontrol,used for all phrase by setting 
//      
// Modify Log:
//            
//           
//	
// Copyright:ASL
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Auto.Data.Common;
using Auto.Web.Common;


namespace Auto.BusMan.UserControl
{
    
    public partial class ucPhraseDropDownList : System.Web.UI.UserControl
    {
        #region --Property--
        /// <summary>
        /// Phrase Class(PhraseID)
        /// </summary>
        //public Phrase PhraseClass
        //{
        //    get { return (ViewState["PhraseClass"] == null) ? Phrase.PMDBType : (Phrase)ViewState["PhraseClass"]; }
        //    set { ViewState["PhraseClass"] = value; }
        //}

        public string PhraseClass
        {
            get { return (ViewState["PhraseClass"] == null) ? "" : (string)ViewState["PhraseClass"]; }
            set { ViewState["PhraseClass"] = value; }
        }

        //public bool HasValue
        //{
        //    get { return (ViewState["HasValue"] == null) ? false : Convert.ToBoolean(ViewState["HasValue"]); }
        //    set { ViewState["HasValue"] = value; }
        //}

        /// <summary>
        /// DropDownList
        /// </summary>
        public DropDownList DropDownList
        {
            get { return ddlPhrase; }
        }

        /// <summary>
        /// Width of DropDownList
        /// </summary>
        public Unit Width
        {
            set { ddlPhrase.Width =  value ; }
        }

        /// <summary>
        /// Enable
        /// </summary>
        public bool Enabled
        {
            get { return (ViewState["Enabled"] == null) ? false : (Boolean)ViewState["Enabled"]; }
            set
            {
                ViewState["Enabled"] = value;
                ddlPhrase.Enabled = value;
            }
        }

        /// <summary>
        /// SelectedValue of DropDownList
        /// </summary>
        public string SelectedValue
        {
            get { return ddlPhrase.SelectedValue; }
            set
            {
                if (ddlPhrase.Items.Contains(ddlPhrase.Items.FindByValue(value)))
                {
                    ddlPhrase.SelectedValue = value;
                }
                else
                {
                     ddlPhrase.SelectedIndex = 0;
                    //ddlPhrase.SelectedValue = "";
                }
            }
        }

        /// <summary>
        /// SelectedText of DropDownList
        /// </summary>
        public string SelectedText
        {
            get { return (ddlPhrase.SelectedItem == null) ? "" : ddlPhrase.SelectedItem.Text; }

            set
            {
                if (ddlPhrase.Items.Contains(ddlPhrase.Items.FindByText(value)))
                {
                    ddlPhrase.SelectedIndex = ddlPhrase.Items.IndexOf(ddlPhrase.Items.FindByText(value));
                }
                else
                {
                    //ddlPhrase.SelectedIndex = 0;
                    ddlPhrase.SelectedValue = "";
                }
            }
        }
        #endregion --Property--

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack /*&& HasValue*/)
            //{
            //    InitControl();
            //}
        }

        /// <summary>
        /// Fill the Data to the Control
        /// </summary>
        public void InitControl()
        {
            IList<SysPhrase.SysPhraseDetail> phraseDetail = new SysPhrase(SessionBox.CurrentSystem, PhraseClass.ToString()).ListDetails;
            BindddlPhrase(phraseDetail);
            foreach (SysPhrase.SysPhraseDetail model in phraseDetail)
            {
                if (model.IsDefault)
                {
                    SelectedValue = model.PhraseValue;
                    break;
                }
            }
        }

        /// <summary>
        /// BindData to Control
        /// </summary>
        /// <param name="phraseDetail">ILIST<SysPhrase.SysPhraseDetail></param>
        private void BindddlPhrase(IList<SysPhrase.SysPhraseDetail> phraseDetail)
        {

            ddlPhrase.DataSource = phraseDetail;
            ddlPhrase.DataTextField = SysPhrase.SysPhraseDetail.FIELD_PhraseText;
            ddlPhrase.DataValueField = SysPhrase.SysPhraseDetail.FIELD_PhraseValue;
            ddlPhrase.DataBind();
        }
    }
}